package com.avecsys.quotemanager.service.supplier;

import java.sql.Blob;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.icefaces.ace.component.fileentry.FileEntryResults.FileInfo;

import com.avecsys.quotemanager.dao.common.EnumDBRecordStatus;
import com.avecsys.quotemanager.dao.common.EnumTransactionStatus;
import com.avecsys.quotemanager.dao.supplier.QuoteTemplateMasterDAO;
import com.avecsys.quotemanager.exception.BusinessException;
import com.avecsys.quotemanager.exception.DAOException;
import com.avecsys.quotemanager.pojos.QuoteTemplateMasterPojo;
import com.avecsys.quotemanager.pojos.SupplierMasterPojo;
import com.avecsys.quotemanager.utils.TransactionStatus;
import com.avecsys.quotemanager.vo.SupplierVO;

/**
 * @author Sam Sundar K
 */
public class QuoteTemplateMasterServiceImpl implements QuoteTemplateMasterService
{

	private static final Log log = LogFactory.getLog(QuoteTemplateMasterService.class);

	private QuoteTemplateMasterDAO quoteTemplateMasterDAO;

	/**
	 * Method to mark as delete the QuoteTemplateMaster Information
	 * 
	 * @param supplierMasterPojo
	 * @return TransactionStatus
	 * @throws BusinessException
	 */
	@Override
	public TransactionStatus deleteQuoteTemplateMaster(QuoteTemplateMasterPojo quoteTemplateMasterPojo)
			throws BusinessException
	{
		TransactionStatus transactionStatus = new TransactionStatus(
				EnumTransactionStatus.DELETE_FAIL.ordinal());
		try
		{
			transactionStatus = quoteTemplateMasterDAO.markAsDeleted(quoteTemplateMasterPojo);
		}
		catch (DAOException e)
		{
			throw new BusinessException(e);
		}
		return transactionStatus;
	}

	@Override
	public List<QuoteTemplateMasterPojo> getAllSupplierQuoteTemplates(
			SupplierMasterPojo supplierMasterPojo) throws BusinessException
	{
		try
		{
			return quoteTemplateMasterDAO.findAllSupplierQuoteTemplates(supplierMasterPojo);
		}
		catch (DAOException e)
		{
			throw new BusinessException(e);
		}

	}

	/**
	 * Method to get QuoteTemplateMaster information by id
	 * 
	 * @param templateId
	 * @return QuoteTemplateMasterPojo
	 * @throws BusinessException
	 */
	@Override
	public QuoteTemplateMasterPojo getQuoteTemplateMasterById(Long templateId) throws BusinessException
	{
		QuoteTemplateMasterPojo quoteTemplateMasterPojo = new QuoteTemplateMasterPojo();
		try
		{
			quoteTemplateMasterPojo = (QuoteTemplateMasterPojo) quoteTemplateMasterDAO.findActiveById(
					quoteTemplateMasterPojo,
						templateId);
		}
		catch (DAOException e)
		{
			throw new BusinessException(e);
		}
		return quoteTemplateMasterPojo;

	}

	/**
	 * @return the quoteTemplateMasterDAO
	 */
	public QuoteTemplateMasterDAO getQuoteTemplateMasterDAO()
	{
		return quoteTemplateMasterDAO;
	}

	/**
	 * Method to check whether the same template name already exists for this supplier
	 * 
	 * @param supplierVO
	 * @return boolean
	 * @throws BusinessException
	 */
	@Override
	public boolean isQuoteTemplateNameUnique(SupplierVO supplierVO) throws BusinessException
	{
		boolean isQuoteTemplateNameUnique = true;
		try
		{
			List<QuoteTemplateMasterPojo> quoteTemplateMasterPojoList = quoteTemplateMasterDAO.findAllActiveQuoteTemplatesByName(
					supplierVO.getSupplierMasterPojo(),
						supplierVO.getTemplateName());
			if (quoteTemplateMasterPojoList.size() > 0)
			{
				for (QuoteTemplateMasterPojo quoteTemplateMasterPojo : quoteTemplateMasterPojoList)
				{
					if (supplierVO.getTemplateId() != null
							&& (supplierVO.getTemplateId().longValue() != quoteTemplateMasterPojo.getTemplateId()
									.longValue()))
					{
						isQuoteTemplateNameUnique = false;
					}
					else if (supplierVO.getTemplateId() == null
							&& supplierVO.getTemplateName().equalsIgnoreCase(
									quoteTemplateMasterPojo.getTemplateName()))
					{
						isQuoteTemplateNameUnique = false;
					}
				}
			}
		}
		catch (DAOException e)
		{
			throw new BusinessException(e);
		}
		return isQuoteTemplateNameUnique;
	}

	/**
	 * Method to save or update the supplier Quote Template
	 * 
	 * @param supplierVO
	 * @return TransactionStatus
	 * @throws BusinessException
	 */
	@Override
	public TransactionStatus saveQuoteTemplateMaster(SupplierVO supplierVO) throws BusinessException
	{
		// Initial Transaction state when save operation
		TransactionStatus transactionStatus = new TransactionStatus(
				EnumTransactionStatus.SAVE_FAIL.ordinal());
		try
		{
			// Step1:
			// if the Quote Template Master Information information already persisted then update
			// other wise create a new supplier information with default use
			// Mark As Active
			supplierVO.getQuoteTemplateMasterPojo().setStatus(EnumDBRecordStatus.ACTIVE.status());
			if (supplierVO.getQuoteTemplateMasterPojo().isPersisted())
			{
				// Initial Transaction State when update operation
				transactionStatus.setStatus(EnumTransactionStatus.UPDATE_FAIL.ordinal());

				FileInfo quoteAttachmentFileInfo = supplierVO.getQuoteAttachmentFileInfo();
				if (quoteAttachmentFileInfo != null)
				{
					supplierVO.getQuoteTemplateMasterPojo().setFileName(
							quoteAttachmentFileInfo.getFileName());
					supplierVO.getQuoteTemplateMasterPojo().setContentType(
							quoteAttachmentFileInfo.getContentType());
					Blob blobFileContent = quoteTemplateMasterDAO.getBlob(quoteAttachmentFileInfo.getFile());
					supplierVO.getQuoteTemplateMasterPojo().setFileContent(blobFileContent);
				}
				transactionStatus = quoteTemplateMasterDAO.updateQuoteTemplateMaster(supplierVO.getQuoteTemplateMasterPojo());
			}
			else
			{
				// Convert File Attachment Object to Logical Blob
				FileInfo quoteAttachmentFileInfo = supplierVO.getQuoteAttachmentFileInfo();
				supplierVO.getQuoteTemplateMasterPojo().setFileName(
						quoteAttachmentFileInfo.getFileName());
				supplierVO.getQuoteTemplateMasterPojo().setContentType(
						quoteAttachmentFileInfo.getContentType());
				Blob blobFileContent = quoteTemplateMasterDAO.getBlob(quoteAttachmentFileInfo.getFile());
				supplierVO.getQuoteTemplateMasterPojo().setFileContent(blobFileContent);
				// Create New Quote Template
				transactionStatus = quoteTemplateMasterDAO.saveQuoteTemplateMaster(supplierVO.getQuoteTemplateMasterPojo());
			}
		}
		catch (DAOException e)
		{
			log.error(e);
			throw new BusinessException(e);
		}
		return transactionStatus;
	}

	/**
	 * @param quoteTemplateMasterDAO the quoteTemplateMasterDAO to set
	 */
	public void setQuoteTemplateMasterDAO(QuoteTemplateMasterDAO quoteTemplateMasterDAO)
	{
		this.quoteTemplateMasterDAO = quoteTemplateMasterDAO;
	}
}
